﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace ConsoleApp1
{
    class Program
    {
        static List<Point> points = new List<Point>();
        static void Main(string[] args)
        {
            List<Point> outpoints = new List<Point>();
            points.Add(new Point(4, 9));
            points.Add(new Point(3, 7));
            points.Add(new Point(1, 5));
            points.Add(new Point(3, 2));
            points.Add(new Point(2, 1));
            points.Add(new Point(2, 8));

            Console.WriteLine("Sample Function1 Result!");
            Run1(points, out outpoints);
            foreach (Point p in outpoints)
                Console.WriteLine(p.X.ToString() +","+ p.Y.ToString());
            Console.WriteLine("_______________________________________");

            Console.WriteLine("Sample Function2 Result!");
            Run2(points, out outpoints);
            foreach (Point p in outpoints)
                Console.WriteLine(p.X.ToString() + "," + p.Y.ToString());
            Console.WriteLine("_______________________________________");

            Console.WriteLine("Sample Function3 Result!");
            Run3(points, out outpoints);
            foreach (Point p in outpoints)
                Console.WriteLine(p.X.ToString() + "," + p.Y.ToString());
        }

        public static void Run3(List<Point> srcData, out List<Point> destData)
        {
            destData = new List<Point>();
            destData = srcData.OrderBy(p => p.X).ThenBy(p => p.Y).ToList();
        }
        public static void Run2(List<Point> srcData,out List<Point> destData)
        {
            destData = new List<Point>();
            srcData.Sort((P1, P2) => {
                int ret = P1.X.CompareTo(P2.X);
                if (ret == 0)
                {
                    return P1.Y.CompareTo(P2.Y);
                }
                else
                {
                    return ret;
                }
            }
            );
            destData = srcData;
        }

        public static void Run1(List<Point> srcData,out List<Point> destData)
        {
            destData = new List<Point>();
            destData = srcData;
            for (int i = 0; i<srcData.Count; i++)
            {
                for (int j = 0; j < srcData.Count-1; j++)
                {
                    if (srcData[j].X > srcData[j + 1].X)
                    {
                        Point temp;
                        temp = srcData[j];
                        srcData[j] = srcData[j + 1];
                        srcData[j+1] = temp;
                    }
                    else if (srcData[j].X == srcData[j + 1].X)
                    {
                        if (srcData[j].Y > srcData[j + 1].Y)
                        {
                            Point temp;
                            temp = srcData[j];
                            srcData[j] = srcData[j + 1];
                            srcData[j+1] = temp;
                        }
                    }
                }
            }
            destData = srcData;
        }
    }
}
